package edu.ccut.saturn.component.impl.node;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.ILogicContext;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.ITransformContent;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDebugThreadsManager;
import edu.ccut.saturn.component.impl.SaturnParameterInfo;
import edu.ccut.saturn.component.loader.node.NodeInfoLoader;
import edu.ccut.saturn.component.loader.node.TransformNodeInfoLoader;
import edu.ccut.saturn.component.utils.ExceptionUtils;
import edu.ccut.saturn.component.view.XmlView;
import edu.ccut.saturn.component.xml.ISaturnElement;
import edu.ccut.saturn.component.xml.SaturnElement;

/**
 * 转移结点的实现类。转移结点位于action组件中，能够把内存中的一个或多个对象生成为指定格式的xml数据描述。
 * <p>
 * 共支持4种类型的数据转换成xml描述:
 * <li>简单数据类型“data”：生成的xml格式为<datas><value key=""></value></datas>。其中属性key的值是待转换格式的变量名称，
 * 结点value的值是变量的值的字符串表示。
 * <li>SaturnData类型“saturnData”：生成的xml格式为<datas><map key="待转换数据的变量名"><value
 * key="saturnData中的属性名">属性值</value></datas>.
 * <li>map类型“map”：生成的xml格式同SaturnData类型转换的xml格式相同。
 * <li>list类型“list”：这里的list中的元素必须是SaturnData类型。生成的xml格式为<datas><list key=""><map
 * key=""></map></datas> 一个转移结点可以一次生成多个不同类型的数据的xml表示。
 * <p>
 * 在生成SaturnData的xml描述时，如果某个字段引用了数据字典，则需要在saturnData对象中添加以字段名加“_dict”结束的新字段，用来保存字典值，并添加到xml描述中。
 * <p>
 * <code>DataTransformNodeInfo</code>扩展了<code>AbstractNodeInfo</code>,并记录了用于转换类型和生成的xml数据的结点和属性名称:
 * <li>字段contents记录了待转换的数据和要转换成的xml格式的信息。
 * <li>字段valueKey记录了生成的xml格式的数据在上下文中存放的位置，以便其它结点可以使用。
 * <li>字段type记录要转换的类型，支持xml, json
 * <li>转移结点一般和xml类型的视图结点一同使用，用于生成ajax请求的响应数据。 TODO：这个结点和视图结点结合
 * 
 * @author 温建
 * @version 1.0 2008.8.20
 * 
 * @see AbstractNodeInfo
 * @see ITransformContent
 * @see XmlView
 */
public class DataTransformNodeInfo extends AbstractNodeInfo {

	// 默认类型 private static final String TYPE_XML = "xml";

	private static final String TYPE_JSON = "json";

	private static final String TYPE_JSON_ARRAY = "json_array";

	private static final String DICT_POSTFIX = "_dict";

	private static final String KEY_NODE_VALUE = "value";

	private static final String KEY_NODE_KEY = "key";

	private static final String KEY_NODE_MAP = "map";

	private static final String KEY_NODE_LIST = "list";

	private static final String TRANSFORM_TYPE_DATA = "data";

	private static final String TRANSFORM_TYPE_LIST = "list";

	private static final String TRANSFORM_TYPE_SATURNDATA = "saturnData";

	private static final String TRANSFORM_TYPE_MAP = "map";

	private static final String KEY_NODE_ROOT = "datas";

	private List<ITransformContent> contents;

	private String valueKey;

	private String valueType;

	public DataTransformNodeInfo(String id, String name, String description,
			String nextId, List<ITransformContent> content, String valueKey,
			String valueType, String x, String y) {

		super(id, name, description, nextId, null, x, y, "transform");
		this.contents = content;
		this.valueKey = valueKey;
		this.valueType = valueType;
	}

	/**
	 * 转移结点的执行逻辑。首先生成xml的根节点，然后逐个转换用户配置的信息。最后把 生成的xml放到上下文中。
	 * 
	 * @param logicContext
	 *            逻辑上下文对象，记录结点运行时的上下文信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void doExecute(ILogicContext logicContext) throws Exception {
		ComponentManager.beforeExecuteComponent(this, this
				.createParameterContext(logicContext));

		try {
			String value = "";

			if (TYPE_JSON.equals(this.valueType)) {
				value = getJSONValue(logicContext);
				logger.debug("      node : {execute: 'DataTransform to JSon: " + value + "', process : '" + logicContext.getId() + "'}");
			} else if (TYPE_JSON_ARRAY.equals(this.valueType)) {
				value = getJSONValueArray(logicContext);
				logger.debug("      node : {execute: 'DataTransform to JSonArray:" + value+ "', process : '" + logicContext.getId() + "'}");
			} else {
				value = this.getXMLValue(logicContext);
				logger.debug("      node : {execute: 'DataTransform to XML:\n" + value+ "', process : '" + logicContext.getId() + "'}");
			}

			logicContext.setValue(this.valueKey, value);
		} catch (Exception e) {
			logger.error("ERROR: {messsage : '" + ExceptionUtils.getExMessage(e) + "', process : '" + logicContext.getId() + "', node : '"+ super.getNodeType() + " " + super.getId() + " " + super.getName()+ "'}");
			throw e;
		} finally {
			ComponentManager.afterExecuteComponent(this, this
					.createParameterContext(logicContext));
		}
	}

//	public static String toUnicode(String value) {
//		StringBuffer unicodeBytes = new StringBuffer();
//		for (int byteIndex = 0; byteIndex < value.length(); byteIndex++) {
//			String hexB = Integer.toHexString(value.charAt(byteIndex));
//			unicodeBytes.append("\\u");
//			if (hexB.length() <= 2) {
//				unicodeBytes.append("00");
//			}
//			unicodeBytes.append(hexB);
//		}
//
//		return unicodeBytes.toString();
//	}

	private String getJSONValue(ILogicContext logicContext) {
		JSONObject json = new JSONObject();

		for (ITransformContent contentItem : contents) {
			String key = contentItem.getContentValueKey();
			
			Object value = logicContext.getValue(key);
			if (value != null) {
				iteratorSaturnDataForDict(value);
			} else {
				int index = key.indexOf("=");
				if (index > 0 && index < key.length() - 1) {
					value = key.substring(index + 1).trim();
					key = key.substring(0, index).trim();
				}
			}
			try {
				if(value != null){
					json.put(key, value);
				} else {
					json.put(key, "");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return json.toString();
	}
	
	@SuppressWarnings("unchecked")
	private String getJSONValueArray(ILogicContext logicContext) {
		JSONArray json = new JSONArray();

		for (ITransformContent contentItem : contents) {
			String key = contentItem.getContentValueKey();
			
			Object value = logicContext.getValue(key);
			if (value != null) {
				iteratorSaturnDataForDict(value);
				
				if (value instanceof List) {
					for (Object object : (List)value) {
						if (object != null) {
							json.put(object);
						}
					}
				} else {
					json.put(value);
				}
			}
		}

		return json.toString();
	}
	
	
	@SuppressWarnings("unchecked")
	public void iteratorSaturnDataForDict(Object data) {
		if (data != null) {
			if (data instanceof SaturnData) {
				addDictToSaturnData((SaturnData) data);
			} else if (data instanceof List) {
				for (Object value : (List) data) {
					iteratorSaturnDataForDict(value);
				}
			} else if (data instanceof Map) {
				for (Object value : ((Map) data).values()) {
					iteratorSaturnDataForDict(value);
				}
			}
		}
	}

	public void addDictToSaturnData(SaturnData saturnData) {
		IDataInfo dataInfo = saturnData.getSaturnDataInfo();

		for (String dataItemKey : dataInfo.keySet()) {
			IDataItem dataItem = dataInfo.getDataItem(dataItemKey);

			if (saturnData.get(dataItemKey) != null
					&& !"".equals(saturnData.get(dataItemKey))
					&& dataItem.getDictTypeId() != null
					&& !"".equals(dataItem.getDictTypeId())) {

				String key = dataItemKey + DICT_POSTFIX;
				Object dictValue = saturnData.get(key);

				saturnData.put(key, dictValue);
			}
		}
	}

	private String getXMLValue(ILogicContext logicContext) {
		StringBuffer stringbuffer = new StringBuffer();
		stringbuffer.append("<");
		stringbuffer.append(KEY_NODE_ROOT);
		stringbuffer.append(">");

		for (ITransformContent contentItem : contents) {

			if (TRANSFORM_TYPE_DATA.equals(contentItem.getType())) {
				getTransData(logicContext, stringbuffer, contentItem);
			} else if (TRANSFORM_TYPE_SATURNDATA.equals(contentItem.getType())) {
				getTransSaturnData(logicContext, stringbuffer, contentItem);
			} else if (TRANSFORM_TYPE_MAP.equals(contentItem.getType())) {
				getTransMap(logicContext, stringbuffer, contentItem);
			} else if (TRANSFORM_TYPE_LIST.equals(contentItem.getType())) {
				getTransList(logicContext, stringbuffer, contentItem);
			}
		}
		stringbuffer.append("</");
		stringbuffer.append(KEY_NODE_ROOT);
		stringbuffer.append(">");

		return stringbuffer.toString();
	}

	private void getTransSaturnData(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		SaturnData saturnData = (SaturnData) logicContext.getValue(contentItem
				.getContentValueKey());

		if (saturnData != null) {
			stringbuffer.append("<");
			stringbuffer.append(KEY_NODE_MAP);
			stringbuffer.append(" ");
			stringbuffer.append(KEY_NODE_KEY);
			stringbuffer.append("=\"" + contentItem.getContentValueKey()
					+ "\">");

			IDataInfo dataInfo = saturnData.getSaturnDataInfo();

			for (String dataItemKey : dataInfo.keySet()) {
				stringbuffer.append("<");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(" ");
				stringbuffer.append(KEY_NODE_KEY);
				stringbuffer.append("=\"");

				if (contentItem.getPrefix() != null
						&& !"".equals(contentItem.getPrefix())) {

					stringbuffer.append(contentItem.getPrefix() + "_"
							+ dataItemKey);

				} else {
					stringbuffer.append(dataItemKey);
				}
				stringbuffer.append("\"");
				stringbuffer.append(">");
				Object value = saturnData.get(dataItemKey);

				if (value != null) {
					stringbuffer.append(getValue(value.toString()));
				}
				stringbuffer.append("</");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(">");
				IDataItem dataItem = dataInfo.getDataItem(dataItemKey);

				if (saturnData.get(dataItemKey) != null
						&& !"".equals(saturnData.get(dataItemKey))
						&& dataItem.getDictTypeId() != null
						&& !"".equals(dataItem.getDictTypeId())) {

					stringbuffer.append("<");
					stringbuffer.append(KEY_NODE_VALUE);
					stringbuffer.append(" ");
					stringbuffer.append(KEY_NODE_KEY);
					stringbuffer.append("=\"");
					stringbuffer.append(dataItemKey + DICT_POSTFIX);
					stringbuffer.append("\">");
					Object dictValue = saturnData
							.get(dataItemKey + DICT_POSTFIX);

					if (dictValue != null) {
						stringbuffer.append(getValue(dictValue.toString()));
					}
					stringbuffer.append("</");
					stringbuffer.append(KEY_NODE_VALUE);
					stringbuffer.append(">");
				}
			}
			stringbuffer.append("</");
			stringbuffer.append(KEY_NODE_MAP);
			stringbuffer.append(">");

		} else {
			logger
					.warn("execute the datatransformNode: can not parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]! the value of the key in context is null!");
		}
	}

	@SuppressWarnings("unchecked")
	private void getTransMap(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		Map<String, Object> originalMap = (Map<String, Object>) logicContext
				.getValue(contentItem.getContentValueKey());

		if (originalMap != null) {
			stringbuffer.append("<");
			stringbuffer.append(KEY_NODE_MAP);
			stringbuffer.append(" ");
			stringbuffer.append(KEY_NODE_KEY);
			stringbuffer.append("=\"" + contentItem.getContentValueKey()
					+ "\">");

			for (String key : originalMap.keySet()) {
				stringbuffer.append("<");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(" ");
				stringbuffer.append(KEY_NODE_KEY);
				stringbuffer.append("=\"");

				if (contentItem.getPrefix() != null
						&& !"".equals(contentItem.getPrefix())) {

					stringbuffer.append(contentItem.getPrefix() + "_" + key);

				} else {
					stringbuffer.append(key);
				}
				stringbuffer.append("\">");
				Object value = originalMap.get(key);

				if (value != null) {
					stringbuffer.append(getValue(value.toString()));
				}
				stringbuffer.append("</");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(">");
			}
			stringbuffer.append("</");
			stringbuffer.append(KEY_NODE_MAP);
			stringbuffer.append(">");

		} else {
			logger
					.warn("execute the datatransformNode: can not parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]! the value of the key in context is null!");
		}
	}

	@SuppressWarnings("unchecked")
	private void getTransList(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		List originalList = (List) logicContext.getValue(contentItem
				.getContentValueKey());

		if (originalList != null && !originalList.isEmpty()) {

			if (originalList.get(0) instanceof SaturnData) {
				getTransSaturnDataList(logicContext, stringbuffer, contentItem);

			} else if (originalList.get(0) instanceof Map) {
				getTransMapList(logicContext, stringbuffer, contentItem);

			} else {
				getTransDataList(logicContext, stringbuffer, contentItem);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void getTransMapList(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		List<Map<String, Object>> originalList = (List<Map<String, Object>>) logicContext
				.getValue(contentItem.getContentValueKey());

		if (originalList != null) {

			if (originalList.size() > 0) {
				stringbuffer.append("<");
				stringbuffer.append(KEY_NODE_LIST);
				stringbuffer.append(" ");
				stringbuffer.append(KEY_NODE_KEY);
				stringbuffer.append("=\"");
				stringbuffer.append(contentItem.getContentValueKey());
				stringbuffer.append("\">");
				int i = 0;

				for (Map<String, Object> map : originalList) {
					stringbuffer.append("<");
					stringbuffer.append(KEY_NODE_MAP);
					stringbuffer.append(" ");
					stringbuffer.append(KEY_NODE_KEY);
					stringbuffer.append("=\"" + (i++) + "\">");

					for (String key : map.keySet()) {
						stringbuffer.append("<");
						stringbuffer.append(KEY_NODE_VALUE);
						stringbuffer.append(" ");
						stringbuffer.append(KEY_NODE_KEY);
						stringbuffer.append("=\"");

						if (contentItem.getPrefix() != null
								&& !"".equals(contentItem.getPrefix())) {

							stringbuffer.append(contentItem.getPrefix() + "_"
									+ key);

						} else {
							stringbuffer.append(key);
						}
						stringbuffer.append("\">");
						Object value = map.get(key);

						if (value != null) {
							stringbuffer.append(getValue(value.toString()));
						}
						stringbuffer.append("</");
						stringbuffer.append(KEY_NODE_VALUE);
						stringbuffer.append(">");
					}
					stringbuffer.append("</");
					stringbuffer.append(KEY_NODE_MAP);
					stringbuffer.append(">");
				}
				stringbuffer.append("</");
				stringbuffer.append(KEY_NODE_LIST);
				stringbuffer.append(">");

			} else {
				logger
						.warn("execute the datatransformNode: parse the map list with key ["
								+ contentItem.getContentValueKey()
								+ "]! the size of the list is 0!");
			}

		} else {
			logger
					.warn("execute the datatransformNode: can not parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]! the value of the key in context is null!");
		}
	}

	@SuppressWarnings("unchecked")
	private void getTransSaturnDataList(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		List<SaturnData> originallist = (List<SaturnData>) logicContext
				.getValue(contentItem.getContentValueKey());

		if (originallist != null) {

			if (originallist.size() > 0) {
				stringbuffer.append("<");
				stringbuffer.append(KEY_NODE_LIST);
				stringbuffer.append(" ");
				stringbuffer.append(KEY_NODE_KEY);
				stringbuffer.append("=\"");
				stringbuffer.append(contentItem.getContentValueKey());
				stringbuffer.append("\">");
				int i = 0;

				for (SaturnData saturnData : originallist) {
					stringbuffer.append("<");
					stringbuffer.append(KEY_NODE_MAP);
					stringbuffer.append(" ");
					stringbuffer.append(KEY_NODE_KEY);
					stringbuffer.append("=\"" + (i++) + "\">");
					IDataInfo dataInfo = saturnData.getSaturnDataInfo();

					for (String dataItemKey : dataInfo.keySet()) {
						stringbuffer.append("<");
						stringbuffer.append(KEY_NODE_VALUE);
						stringbuffer.append(" ");
						stringbuffer.append(KEY_NODE_KEY);
						stringbuffer.append("=\"");

						if (contentItem.getPrefix() != null
								&& !"".equals(contentItem.getPrefix())) {

							stringbuffer.append(contentItem.getPrefix() + "_"
									+ dataItemKey);

						} else {
							stringbuffer.append(dataItemKey);
						}
						stringbuffer.append("\"");
						stringbuffer.append(">");
						Object value = saturnData.get(dataItemKey);

						if (value != null) {
							stringbuffer.append(getValue(value.toString()));
						}
						stringbuffer.append("</");
						stringbuffer.append(KEY_NODE_VALUE);
						stringbuffer.append(">");
						IDataItem dataItem = dataInfo.getDataItem(dataItemKey);

						if (saturnData.get(dataItemKey) != null
								&& !"".equals(saturnData.get(dataItemKey))
								&& dataItem.getDictTypeId() != null
								&& !"".equals(dataItem.getDictTypeId())) {

							stringbuffer.append("<");
							stringbuffer.append(KEY_NODE_VALUE);
							stringbuffer.append(" ");
							stringbuffer.append(KEY_NODE_KEY);
							stringbuffer.append("=\"");
							stringbuffer.append(dataItemKey + DICT_POSTFIX);
							stringbuffer.append("\">");
							Object dictValue = saturnData.get(dataItemKey
									+ DICT_POSTFIX);

							if (dictValue != null) {
								stringbuffer.append(getValue(dictValue
										.toString()));
							}
							stringbuffer.append("</");
							stringbuffer.append(KEY_NODE_VALUE);
							stringbuffer.append(">");
						}
					}
					stringbuffer.append("</");
					stringbuffer.append(KEY_NODE_MAP);
					stringbuffer.append(">");
				}
				stringbuffer.append("</");
				stringbuffer.append(KEY_NODE_LIST);
				stringbuffer.append(">");

			} else {
				logger
						.warn("execute the datatransformNode: parse the saturndata list with key ["
								+ contentItem.getContentValueKey()
								+ "]! the size of the list is 0!");
			}

		} else {
			logger
					.warn("execute the datatransformNode: can not parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]! the value of the key in context is null!");
		}
	}

	@SuppressWarnings("unchecked")
	private void getTransDataList(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		List originallist = (List) logicContext.getValue(contentItem
				.getContentValueKey());

		stringbuffer.append("<");
		stringbuffer.append(KEY_NODE_LIST);
		stringbuffer.append(" ");
		stringbuffer.append(KEY_NODE_KEY);
		stringbuffer.append("=\"");
		stringbuffer.append(contentItem.getContentValueKey());
		stringbuffer.append("\">");

		if (originallist != null) {

			for (Object item : originallist) {
				stringbuffer.append("<");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(">");
				Object value = item;

				if (value != null) {
					stringbuffer.append(getValue(value.toString()));
				}
				stringbuffer.append("</");
				stringbuffer.append(KEY_NODE_VALUE);
				stringbuffer.append(">");
			}

		} else {
			logger
					.warn("execute the datatransformNode: can not parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]! the value of the key in context is null!");
		}
		stringbuffer.append("</");
		stringbuffer.append(KEY_NODE_LIST);
		stringbuffer.append(">");
	}

	private void getTransData(ILogicContext logicContext,
			StringBuffer stringbuffer, ITransformContent contentItem) {

		// TODO：SaturnData怎么办？等于null怎么办？
		Object originalData = logicContext.getValue(contentItem
				.getContentValueKey());

		stringbuffer.append("<");
		stringbuffer.append(KEY_NODE_VALUE);
		stringbuffer.append(" ");
		stringbuffer.append(KEY_NODE_KEY);
		stringbuffer.append("=\"" + contentItem.getContentValueKey() + "\">");

		if (originalData != null) {
			stringbuffer.append(getValue(originalData.toString()));

		} else {
			logger
					.warn("execute the datatransformNode: parse the data with key ["
							+ contentItem.getContentValueKey()
							+ "]; the value of the key in context is null!");
		}
		stringbuffer.append("</");
		stringbuffer.append(KEY_NODE_VALUE);
		stringbuffer.append(">");
	}

	/**
	 * 获得用户配置的需要转换xml表示的数据信息列表
	 * 
	 * @return
	 */
	public List<ITransformContent> getContent() {
		return contents;
	}

	/**
	 * 设置需要转换xml表示的数据信息列表
	 * 
	 * @param content
	 */
	public void setContent(List<ITransformContent> content) {
		this.contents = content;
	}

	/**
	 * 获得生成的xml格式的数据在上下文中的存放位置
	 * 
	 * @return
	 */
	public String getValueKey() {
		return valueKey;
	}

	/**
	 * 设置生成的xml格式的数据在上下文中的存放位置
	 * 
	 * @param valueKey
	 */
	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	private String getValue(String value) {

		if (value != null && !"".equals(value)) {
			String valueStr = value;
			valueStr = valueStr.replace(">", LARGE_THAN);
			valueStr = valueStr.replace("<", LESS_THAN);
			return valueStr;

		} else {
			return value;
		}
	}

	@SuppressWarnings("unchecked")
	protected IParameterContext createParameterContext(
			ILogicContext logicContext) throws Exception {

		if (SaturnDebugThreadsManager.getInstance().isStart()) {
			initParameterInfos();
			return logicContext.createParameterContext(getParameterInfos());
		}
		return null;
	}

	private void initParameterInfos() {
		int parameterIndex = 1;
		SaturnParameterInfo resultParameterInfo = new SaturnParameterInfo();
		resultParameterInfo.setClassType(String.class);
		resultParameterInfo.setName("result");
		resultParameterInfo.setId(String.valueOf(parameterIndex));
		resultParameterInfo.setValueKey(this.getValueKey());
		resultParameterInfo.setScope(IParameterInfo.SCOPE_LOGIC);
		resultParameterInfo.setPassingType(IParameterInfo.PASSING_IN_OUT);
		List<IParameterInfo> parameters = new ArrayList<IParameterInfo>();
		parameters.add(resultParameterInfo);

		for (ITransformContent contentItem : contents) {
			parameterIndex++;

			if (TRANSFORM_TYPE_DATA.equals(contentItem.getType())) {
				SaturnParameterInfo contentParameterInfo = new SaturnParameterInfo();
				contentParameterInfo.setClassType(Object.class);
				contentParameterInfo.setName("content1");
				contentParameterInfo.setValueKey(contentItem
						.getContentValueKey());

				contentParameterInfo.setId(String.valueOf(parameterIndex));
				contentParameterInfo.setScope(IParameterInfo.SCOPE_LOGIC);
				contentParameterInfo.setPassingType(IParameterInfo.PASSING_IN);
				parameters.add(contentParameterInfo);
			}

			if (TRANSFORM_TYPE_SATURNDATA.equals(contentItem.getType())) {
				SaturnParameterInfo contentParameterInfo = new SaturnParameterInfo();
				contentParameterInfo.setClassType(SaturnData.class);
				contentParameterInfo.setName("content2");
				contentParameterInfo.setValueKey(contentItem
						.getContentValueKey());

				contentParameterInfo.setId(String.valueOf(parameterIndex));
				contentParameterInfo.setScope(IParameterInfo.SCOPE_LOGIC);
				contentParameterInfo.setPassingType(IParameterInfo.PASSING_IN);
				parameters.add(contentParameterInfo);
			}

			if (TRANSFORM_TYPE_MAP.equals(contentItem.getType())) {
				SaturnParameterInfo contentParameterInfo = new SaturnParameterInfo();
				contentParameterInfo.setClassType(Map.class);
				contentParameterInfo.setName("content3");
				contentParameterInfo.setValueKey(contentItem
						.getContentValueKey());

				contentParameterInfo.setId(String.valueOf(parameterIndex));
				contentParameterInfo.setScope(IParameterInfo.SCOPE_LOGIC);
				contentParameterInfo.setPassingType(IParameterInfo.PASSING_IN);
				parameters.add(contentParameterInfo);
			}

			if (TRANSFORM_TYPE_LIST.equals(contentItem.getType())) {
				SaturnParameterInfo contentParameterInfo = new SaturnParameterInfo();
				contentParameterInfo.setClassType(List.class);
				contentParameterInfo.setName("content4");
				contentParameterInfo.setValueKey(contentItem
						.getContentValueKey());

				contentParameterInfo.setId(String.valueOf(parameterIndex));
				contentParameterInfo.setScope(IParameterInfo.SCOPE_LOGIC);
				contentParameterInfo.setPassingType(IParameterInfo.PASSING_IN);
				parameters.add(contentParameterInfo);
			}
		}

		super.setParameterInfos(parameters);
	}

	public ISaturnElement toSaturnElement() {
		ISaturnElement dataTransformElment = new SaturnElement(
				TransformNodeInfoLoader.KEY_NODETYPE_TRANSFORM);

		dataTransformElment.addAttribute(NodeInfoLoader.KEY_NODE_ID, this
				.getId());

		dataTransformElment.addAttribute(NodeInfoLoader.KEY_NODE_NEXT, this
				.getNextId());

		dataTransformElment
				.addAttribute(NodeInfoLoader.KEY_NODE_X, this.getX());

		dataTransformElment
				.addAttribute(NodeInfoLoader.KEY_NODE_Y, this.getY());

		ISaturnElement valueKeyElement = new SaturnElement(
				TransformNodeInfoLoader.KEY_TRANSFORM_KEY, this.valueKey);

		ISaturnElement nameElement = new SaturnElement(
				NodeInfoLoader.KEY_NODE_NAME, this.getName());

		ISaturnElement descriptionElement = new SaturnElement(
				NodeInfoLoader.KEY_NODE_DESCRIPTION, this.getDescription());

		ISaturnElement contentsElement = new SaturnElement(
				TransformNodeInfoLoader.KEY_TRANSFORM_CONTENTS);

		if (this.contents != null) {
			int index = 0;

			for (ITransformContent content : this.contents) {
				ISaturnElement contentElement = new SaturnElement(
						TransformNodeInfoLoader.KEY_TRANSFORM_CONTENTS_CONTENT);

				contentElement.addAttribute(
						TransformNodeInfoLoader.KEY_NODE_ID, String
								.valueOf(index++));

				contentElement.addAttribute(
						TransformNodeInfoLoader.KEY_TRANSFORM_CONTENT_TYPE,
						content.getType());

				ISaturnElement contentKeyElement = new SaturnElement(
						TransformNodeInfoLoader.KEY_TRANSFORM_CONTENT_KEY,
						content.getContentValueKey());

				contentElement.addSubElement(contentKeyElement);
				contentsElement.addSubElement(contentElement);
			}
		}
		dataTransformElment.addSubElement(valueKeyElement);
		dataTransformElment.addSubElement(nameElement);
		dataTransformElment.addSubElement(descriptionElement);
		dataTransformElment.addSubElement(contentsElement);
		return dataTransformElment;
	}
}
